package com.mengdie.shuidi.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.mengdie.shuidi.AppContext;
import com.mengdie.shuidi.AppManager;
import com.mengdie.shuidi.OkHeader;
import com.mengdie.shuidi.R;
import com.mengdie.shuidi.event.LineEvent;
import com.mengdie.shuidi.event.RandomEvent;
import com.mengdie.shuidi.model.BaseModel;
import com.mengdie.shuidi.model.LineEntity;
import com.mengdie.shuidi.model.ServerEnetity;
import com.mengdie.shuidi.ui.adapter.LineAdapter;
import com.mengdie.shuidi.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;


public class LineActivity extends BaseActivity {

    @Bind(R.id.rl_generic_back)
    RelativeLayout mRlGenericBack;
    @Bind(R.id.tv_generic_title)
    TextView mTvGenericTitle;
    @Bind(R.id.rv_line)
    RecyclerView mRvLine;

    private String province;
    private String city;

    private List<LineEntity> enetityList = new ArrayList<>();
    private LineAdapter mLineAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_line_line);
        ButterKnife.bind(this);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            province = bundle.getString("province");
            city = bundle.getString("city");
            getLine(province, city);
        }
        init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelTag(this);
    }

    public void init() {
        mTvGenericTitle.setText("线路选择");
    }

    @OnClick(R.id.rl_generic_back)
    public void onClick() {
        onBackPressed();
    }

    public void getLine(final String province, final String city) {
        HashMap<String, String> map = new HashMap<>();
        map.put("page", "1");
        map.put("keyword", "");
        map.put("province", province);
        map.put("city", city);
        map.put("sort", "");
        map.put("platform", "android");
        JSONObject jsonObject = new JSONObject(map);

        OkGo.getInstance().addCommonHeaders(OkHeader.getCommonHeadToken(this)).post(AppContext.getInstance().getBaseUrl() + "server/get_server_list")
                .tag(LineActivity.this)
                .upJson(jsonObject.toString())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {

                        Gson gson = new Gson();

                        BaseModel<ServerEnetity> baseModel = gson.fromJson(s, new TypeToken<BaseModel<ServerEnetity>>() {
                        }.getType());
                        if (baseModel.getRet().equals("0")) {
                            enetityList = baseModel.getData().getLineInfo();
                            mLineAdapter = new LineAdapter(enetityList);
                            mRvLine.setLayoutManager(new LinearLayoutManager(LineActivity.this));
                            mRvLine.setAdapter(mLineAdapter);
                            mRvLine.addOnItemTouchListener(new OnItemClickListener() {
                                @Override
                                public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {

                                }

                                @Override
                                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                    super.onItemClick(adapter, view, position);
                                    LineEntity lineEntity = enetityList.get(position);
                                    EventBus.getDefault().post(new LineEvent(province, city,
                                            lineEntity.getIp(), lineEntity.getId(), lineEntity.getAgreeType(),
                                            "1", null));
                                    EventBus.getDefault().post(new RandomEvent(false, false,true));
                                    AppManager.getAppManager().finishActivity(ProvinceActivity.class);
                                    // AppManager.getAppManager().finishActivity(LineActivity.class);
                                    finish();
                                }
                            });


                        } else {
                            ToastUtil.showToast(baseModel.getMsg());
                        }

                    }
                });


    }
}
